#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
#include "SSD1306Wire.h"
SSD1306Wire display(0x3c, SDA, SCL);//iic_init
// 接收端的MAC地址
uint8_t broadcastAddress[] = {0xEC, 0x94, 0xCB, 0x7C, 0x16, 0x84};//EC:94:CB:7C:16:84

int LX = 32;
int LY = 33;
int RX1 =35;
int RY = 34;
int led_=39;//led 引脚0-4095
int autu_con=13;//au/pc 引脚定义 中间为1 
int seconds=15;
int servoVal;
//幅值变量
int LXVal = 0;
int LYVal = 0;
int RXVal = 0;
int RYVal = 0;
  
int connected_flag=0;

int led_flag=0;
// 发送结构体类型
typedef struct struct_message {
  //发送参量
  int rx;
  int ry;
  int lx;
  int ly;
  int lok;
  int rok; 
  int AUTOjudg;
 // int led_c;
  //回传参量
  int angle;
  int sp1;
  int sp2;
  int ml;
  int mr;
  int so;
  int led; 
  
} struct_message;

// 创建一个结构体变量
struct_message myData;

// 回调函数,函数将在发送消息时执行。此函数告诉我们信息是否成功发送;
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  if(status == ESP_NOW_SEND_SUCCESS)connected_flag=1;
  else connected_flag=0;
}
// 回调函数,当收到消息时会调佣该函数
 void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("Char: ");
  Serial.println(myData.sp1);
  Serial.print("Int: ");
  Serial.println(myData.sp2);
  Serial.print("Float: ");
  Serial.println(myData.mr);
  Serial.print("Bool: ");
  Serial.println(myData.ml);
  Serial.println();
}
 
void setup() {
  // 初始化串口波特率
  Serial.begin(115200);
 
  // 设置WIFI模式为STA模式，即无线终端
  WiFi.mode(WIFI_STA);

  //  初始化ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
 
 //注册回调函数
  esp_now_register_send_cb(OnDataSent);
  esp_now_register_recv_cb(OnDataRecv);
  
  // 注册通信频道
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  //通道
  peerInfo.encrypt = false;//是否加密为False
         
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }

    //iic_oled
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);

  pinMode(13,INPUT_PULLUP);  //配置自动手动模式切换按键所在端口为上拉输入模式
  
}

void ESPNOW_sent(void)
{
  //左手
  LXVal = analogRead(LX);
  Serial.print("X: ");
  Serial.print(LXVal);
  Serial.print(" ");
  LYVal = analogRead(LY);
  Serial.print("Y: ");
  Serial.print(LYVal);
  Serial.print("         ");
  //右手
  RXVal = analogRead(RX1);
  Serial.print("X: ");
  Serial.print(RXVal);
  Serial.print(" ");
  RYVal = analogRead(RY);
  Serial.print("Y: ");
  Serial.print(RYVal);
  Serial.print("       ");
  Serial.println(" (myData.mr： ");
 
 
  delay(10);  

  //AUTOJUDG手动自动切换

  //设置要发送的值
  myData.rx=RXVal;
  myData.ry = RYVal;
  myData.lx= LXVal;
  myData.ly= LYVal;
  myData.AUTOjudg = !digitalRead(13);
  //发送信息到指定ESP32上
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
   
 //判断是否发送成功
  if (result == ESP_OK) {
    Serial.println("Sent with success");
   
  }
  else {
    Serial.println("Error sending the data");
    
  }
}

void get_OLED(void)
{
    display.clear();
    
  /*  display.drawString(70, 5, "OO1:" + String(myData.sp1));  
    display.drawString(20, 5, "OO2:" + String(myData.sp2));  
    display.drawString(70, 15, "Vr:" + String(myData.mr));  
    display.drawString(20, 15, "Vl:" + String(myData.ml));  
    display.drawString(20, 30, "SO:" + String(myData.so));  
    display.drawString(70, 30, "LD:" + String(myData.led));  */

    if (connected_flag == 1){//判断是否连接成功
    /*********///UI界面静态绘画
    display.drawHorizontalLine(10, 1, 20);
    display.drawHorizontalLine(10, 63, 20);
    display.drawVerticalLine(10, 1, 62);
    display.drawVerticalLine(30, 1, 62);//左油门框

    display.drawHorizontalLine(98, 1, 20);
    display.drawHorizontalLine(98, 63, 20);
    display.drawVerticalLine(98, 1, 62);
    display.drawVerticalLine(118, 1, 62);//右油门框

    
    display.drawHorizontalLine(40, 32, 48);
    display.drawVerticalLine(64, 8, 52);//十字线

    display.setFont(ArialMT_Plain_10);//字体大小
    display.drawString(74, 35, "up");
    display.drawString(66, 46, "down");
    display.drawString(66, 6, "on");
    display.drawString(66, 16, "off");
    display.drawString(44, 6, "sp1");
    display.drawString(44, 36, "angle");
 
  /***********/


  /*********///UI动态画面
   
    if(myData.rx<500) led_flag=0;//led判断
    if(myData.rx>3500)led_flag=1;
    if(led_flag==0) display.drawRect(64, 18, 20, 9);//led判断//off
    if(led_flag==1)display.drawRect(64, 8, 15, 9);//on 判断

    if(myData.so==180) display.drawRect(72, 38, 15, 10);//up判断
    else display.drawRect(64, 48, 30, 9);//down判断
    
    display.drawString(44, 16, String(myData.sp1));  //sp1，sp2超声波；//angle
    display.drawString(44, 46, String(myData.angle)); 
    

    if(myData.ml==0) display.drawHorizontalLine(10, 32, 20);//油门动画
    if(myData.ml>0) display.fillRect(10,32-((myData.ml-320)/27) , 20, (myData.ml-320)/27);//实心矩形，x,y,z是x向右长，o是y向下长（）
    if(myData.ml<0) display.fillRect(10, 32, 20, -(myData.ml+320)/27);//实心矩形，x,y,z是x向右长，o是y向下长
    
    if(myData.mr==0) display.drawHorizontalLine(98, 32, 20);
    if(myData.mr>0) display.fillRect(98,32-((myData.mr-320)/27) , 20, (myData.mr-320)/27);//实心矩形，x,y,z是x向右长，o是y向下长（）
    if(myData.mr<0) display.fillRect(98, 32, 20, -(myData.mr+320)/27);//实心矩形，x,y,z是x向右长，o是y向下长

    display.drawString(80, 54, String(myData.mr));  
    display.drawString(30, 54, String(myData.ml)); 
  //display.fillRect(10, 32, 20, 40);//实心矩形，x,y,z是x向右长，o是y向下长

  //手动自动模式转换
  if(myData.AUTOjudg==1){display.setFont(ArialMT_Plain_16);//字体大小
                 display.drawString(56, 22, "Au");}
  if(myData.AUTOjudg==0) {display.setFont(ArialMT_Plain_16);//字体大小
                display.drawString(56, 22, "pc");}
  
display.display();
    
    }

    else {//连接失败判断
      display.clear();
      display.setFont(ArialMT_Plain_24);//字体大小
    display.drawString(2, 15, "Un Connected");
    display.display();
      }
    /*           delay(500);
display.clear();
display.drawHorizontalLine(0, 40, 20);
    display.drawVerticalLine(40, 0, 20);
 display.display();
    */
}
 
void loop() {
  ESPNOW_sent();
  get_OLED();
  delay(10);
}